package com.wx.controller;

import com.wx.dto.CarQueryDTO;
import com.wx.dto.CollectionOrOrderDTO;
import com.wx.dto.ConditionGetCarDTO;
import com.wx.dto.UserByCarDTO;
import com.wx.entity.Car;
import com.wx.entity.User;
import com.wx.result.Result;
import com.wx.service.CarService;
import com.wx.vo.FavorVO;
import com.wx.vo.OrderQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*允许跨域请求*/
@CrossOrigin
@RestController
@Slf4j
public class CarController {
    @Autowired
    private CarService carService;

    /*添加汽车到订单*/
    @GetMapping("/order")
    public Result addOrder(CollectionOrOrderDTO collectionOrOrderDTO){
        log.info("添加汽车到订单：{}",collectionOrOrderDTO);

        carService.order(collectionOrOrderDTO);
        return Result.success();
    }

    /*添加汽车*/
    public Result addCar(Car car){

        return Result.success();
    }

    /*用户提交订单*/
    @PostMapping("/submit")
    public Result submit(@RequestBody UserByCarDTO userByCarDTO){
        log.info("订单id:{}",userByCarDTO);
        return Result.success();
    }

    /*添加汽车到收藏*/
    @PutMapping("/collection")
    public Result usercollection(@RequestBody CollectionOrOrderDTO collectionOrOrderDTO){
        log.info("用户收藏：{}",collectionOrOrderDTO);

        carService.usercollection(collectionOrOrderDTO);
        return Result.success();
    }

    /*获取用户收藏*/
    @GetMapping("/getcollection")
    public Result getcollection(CollectionOrOrderDTO collectionOrOrderDTO){
        log.info("获取用户收藏：{}",collectionOrOrderDTO);

        List<FavorVO> favors = carService.getcollection(collectionOrOrderDTO);

        return Result.success(favors);

    }


    /*用户搜索汽车*/
    @GetMapping("/cars")
    public Result carList(CarQueryDTO carQueryDTO){
        log.info("搜索汽车：{}",carQueryDTO);
        List<Car> cars = carService.carList(carQueryDTO);
        return Result.success(cars);
    }

    /*取消收藏*/
    @PostMapping("/incollection")
    public Result infavor(@RequestBody User user){
        log.info("取消收藏：{}",user);
        carService.infavor(user);
        return Result.success();
    }

    /*获取订单*/
    @GetMapping("/getorder")
    public Result getOrder(User user){
        log.info("获取用户订单：{}",user);
        List<OrderQueryVO> orderQueryVOs = carService.getOrder(user);
        return Result.success(orderQueryVOs);
    }

    /*取消订单*/
    @PostMapping("/delorder")
    public Result delorder(@RequestBody User user){
        log.info("用户取消订单：{}",user);
        carService.delorder(user);
        return Result.success();
    }

    /*条件查询汽车*/
    @GetMapping("/getcar")
    public Result getcarcondition(ConditionGetCarDTO conditionGetCarDTO){
        log.info("条件查询汽车:{}",conditionGetCarDTO);
        List<Car> cars = carService.getcarcondition(conditionGetCarDTO);
        return Result.success(cars);
    }


}
